﻿using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using System;

namespace Ricebird.Wordprocessing
{
    public class WordRun : CompositeElementBase
    {
        private Run Run
        {
            get;set;
        }

        private RunProperties Properties
        {
            get;set;
        }

        public WordRun(Run r, CompositeElementBase @base) : base(@base)
        {
            Run = r;
            InitializeElement();
        }

        public override void InitializeElement()
        {
            Properties = Run.GetFirstChild<RunProperties>();
            if (Properties == null)
            {
                Properties = new RunProperties();
                Run.PrependChild(Properties);
            }
        }

        #region 读取文本
        public void SetText(string text)
        {
            Text t = Run.GetFirstChild<Text>();
            if (t == null)
            {
                t = new Text();
                Run.Append(t);
            }
            t.Text = text;

            if (text.Contains(" "))
            {
                t.Space = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.SpaceProcessingModeValues>(DocumentFormat.OpenXml.SpaceProcessingModeValues.Preserve);
            }
        }

        public string GetText()
        {
            Text t = Run.GetFirstChild<Text>();
            return t?.Text ?? string.Empty;
        }
        #endregion

        #region 流式接口
        public override CompositeElementBase AppendParagraph()
        {
            return Document.AppendParagraph();
        }

        public override CompositeElementBase AppendParagraph(string text)
        {
            return Document.AppendParagraph(text);
        }

        public override CompositeElementBase AppendParagraph(WordStyles style)
        {
            return Document.AppendParagraph(style);
        }

        public override CompositeElementBase AppendParagraph(string text, WordStyles style)
        {
            return Document.AppendParagraph(text, style);
        }

        public override CompositeElementBase Append(string text)
        {
            return BaseElement?.Append(text);
        }

        public override CompositeElementBase Append(string text, WordStyles style)
        {
            return BaseElement?.Append(text, style);
        }
        #endregion

        #region 应用样式
        internal void SetProperties(RunProperties prop)
        {
            Properties = prop;
        }

        public override CompositeElementBase SetFontFamily(string family)
        {
            return SetFontFamily(family, family);
        }

        public override CompositeElementBase SetFontFamily(string zwFamily, string asciiFamily)
        {
            Properties.RunFonts = new RunFonts()
            {
                Ascii = asciiFamily,
                EastAsia = zwFamily
            };
            return this;
        }

        public override CompositeElementBase SetFontSize(int sizePoint)
        {
            if (sizePoint < 0 || sizePoint > 800)
            {
                throw new InvalidOperationException("字体大小必须大于0，并且小于800");
            }

            Properties.FontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize()
            {
                Val = (sizePoint * 2).ToString()
            };
            return this;
        }

        public override CompositeElementBase SetFontSize(FontSize size)
        {
            int sz = (int)size;
            Properties.FontSize = new DocumentFormat.OpenXml.Wordprocessing.FontSize()
            {
                Val = sz.ToString()
            };
            return this;
        }

        public override CompositeElementBase SetBold(bool isBold = true)
        {
            Properties.Bold = new Bold()
            {
                Val = new OnOffValue(isBold)
            };
            return this;
        }

        public override CompositeElementBase SetItalic(bool isItalic = true)
        {
            Properties.Italic = new Italic()
            {
                Val = new OnOffValue(isItalic)
            };
            return this;
        }

        public override CompositeElementBase SetUnderline(UnderlineStyle lineStyle = UnderlineStyle.Single)
        {
            UnderlineValues value = (UnderlineValues)Enum.Parse(typeof(UnderlineValues), lineStyle.ToString());
            Properties.Underline = new Underline()
            {
                Val = value
            };
            return this;
        }

        public override CompositeElementBase SetAlign(TextAlign align = TextAlign.Left)
        {
            BaseElement.SetAlign(align);
            return this;
        }

        public override CompositeElementBase SetFirstLineIndent(double chars = 2)
        {
            BaseElement.SetFirstLineIndent(chars);
            return this;
        }

        public override CompositeElementBase SetLeftChars(double chars = 0)
        {
            BaseElement.SetLeftChars(chars);
            return this;
        }

        public override CompositeElementBase SetRightChars(double chars = 0)
        {
            BaseElement.SetRightChars(chars);
            return this;
        }

        public override CompositeElementBase SetLineSpacing(double times)
        {
            BaseElement.SetLineSpacing(times);
            return this;
        }

        public override CompositeElementBase SpaceingBefore(int pt)
        {
            BaseElement.SpaceingBefore(pt);
            return this;
        }

        public override CompositeElementBase SpacingAfter(int pt)
        {
            BaseElement.SpacingAfter(pt);
            return this;
        }
        #endregion

        #region 流式接口 - 设置样式
        protected override CompositeElementBase SetStyleSafe(WordStyles style)
        {
            Properties.RunStyle = style.RunStyleId;

            return this;
        }
        #endregion
    }
}
